﻿using System;
using System.Collections.Generic;

namespace Accona.Remoting.Interaction
{
    internal class InteractionFactorySingleton : IInteractionFactory
    {
        private static readonly Dictionary<Type, Object> Instances = new Dictionary<Type, object>();

        internal static readonly InteractionFactorySingleton Instance = new InteractionFactorySingleton();

        private InteractionFactorySingleton()
        {
        }

        #region IInteractionFactory Members

        public TInterface CreateInstance<TInterface>(Type implementationType)
        {
            object result;
            if (Instances.TryGetValue(implementationType, out result))
            {
                return (TInterface) result;
            }
            else
            {
                object impl = Activator.CreateInstance(implementationType);
                Instances.Add(typeof (TInterface), impl);
                return (TInterface) impl;
            }
        }

        #endregion
    }
}